package per.zhang.serverPrevioder.controller;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.web.bind.annotation.*;
import per.zhang.serverPrevioder.base.dto.ResultDTO;
import per.zhang.serverPrevioder.base.page.PageList;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import per.zhang.serverPrevioder.base.utils.ResultUtil;
import per.zhang.serverPrevioder.base.utils.VaccineStateCode;
import per.zhang.serverPrevioder.dto.request.VaccineRequsetDTO;
import per.zhang.serverPrevioder.dto.respone.VaccineRespondDTO;
import per.zhang.serverPrevioder.entity.User;
import per.zhang.serverPrevioder.service.UserService;
import per.zhang.serverPrevioder.service.VaccineService;
import per.zhang.serverPrevioder.entity.Vaccine;

import java.util.List;


/**
 *
 * @author Zhang
 * @since 2021-01-25
 */
@RestController
@RequestMapping("/vaccine")
@CrossOrigin
@Slf4j
public class VaccineController {

        @Autowired
        private VaccineService vaccineService;

        @Autowired
        private UserService userService;

        @PostMapping("/selectByUserId")
        @ApiOperation("用户查看疫苗")
        public ResultDTO selectByUserId(@RequestBody VaccineRequsetDTO vaccineRequsetDTO)throws Exception{
            vaccineRequsetDTO.setState(VaccineStateCode.PUTAWAY);
            PageList<VaccineRespondDTO> pageData = null;
            try {
                pageData = vaccineService.seleceByUserId(vaccineRequsetDTO.getUserId(),vaccineRequsetDTO.getPageNum(),vaccineRequsetDTO.getPageSize());
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(pageData);
        }


    @PostMapping("/insertVaccine")
        @ApiOperation(value = "新增疫苗")
        //@ApiImplicitParams()
        public ResultDTO insertOne( @RequestBody Vaccine vaccine)throws Exception{
            try {
                vaccine.setSurplusNum(0);
                vaccine.setAppointmentNum(0);
                vaccine.setState(VaccineStateCode.PUTAWAY);
                vaccineService.save(vaccine);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(vaccine);
        }

        @PostMapping("/updateAppoinmentNumById")
        @ApiOperation(value = "按id修改疫苗可预约量")
        //@ApiImplicitParams()
        public ResultDTO updateSurplusNumById(@RequestBody  Vaccine vaccine)throws Exception{
            try {
                Vaccine saveVaccine  = vaccineService.get(vaccine.getVaccineId());
                if (saveVaccine.getSurplusNum() < vaccine.getAppointmentNum()) {
                    return ResultUtil.Error("500","疫苗余量不足");
                }
                saveVaccine.setAppointmentNum(vaccine.getAppointmentNum());
                saveVaccine.setVaccineId(vaccine.getVaccineId());
                vaccineService.update(saveVaccine);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(vaccineService.get(vaccine.getVaccineId()));

        }

        @PostMapping("/updateById")
        @ApiOperation(value = "按id修改数据")
        //@ApiImplicitParams()
        public ResultDTO updateById( @RequestBody Vaccine vaccine)throws Exception{
            try {
                vaccineService.update(vaccine);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(vaccineService.get(vaccine.getVaccineId()));

        }

        @GetMapping("/deleteById/{id}")
        @ApiOperation("按id删除数据")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name = "id", dataType = "string", required = true, value = "实体类id"),

        })
        public ResultDTO deleteById(@RequestParam(required = true) @PathVariable("id") String id)throws Exception{
            Vaccine oldVaccine = null;
            try {
                oldVaccine = vaccineService.get(id);
                vaccineService.deleteById(id);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(oldVaccine);
        }

        @PostMapping("/getVaccinePage")
        @ApiOperation("按条件查询，分页")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "页码"),
                @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页容量")
        })
        public ResultDTO selectEntityPage(  @RequestBody VaccineRequsetDTO vaccine )throws Exception{
            Vaccine saveVaccine = new Vaccine();
            PropertyUtils.copyProperties(saveVaccine,vaccine);
            PageList<Vaccine> pageData = null;
            try {
                pageData = vaccineService.findPageData(saveVaccine, vaccine.getPageNum(), vaccine.getPageSize());
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(pageData);

        }

        @PostMapping("/getVaccine")
        @ApiOperation("按条件查询，不分页")
        //@ApiImplicitParams()
        public ResultDTO selectEntity( @RequestBody Vaccine vaccine)throws Exception{
            List<Vaccine> vaccineList = null;
            try {
                vaccineList = vaccineService.findByParams(vaccine);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(vaccineList);
        }


}
